<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Image_lib extends CI_Image_lib 
{

    public function __construct()
    {
        parent::__construct();
        ini_set("memory_limit","128M");
    }
    
	public function edit_image($upload_image, $upload_path, $config)
    {
   		//Nazivi datoteka
   		//Resize za thumb
   		$img_resize = $upload_image['raw_name'] . "_resize" . $upload_image['file_ext'];
   		$img_thumb = $upload_image['raw_name'] . "_thumb" . $upload_image['file_ext'];
				
		// Prvo smanji velicinu  slike na duplo vecu od stvarne velicine thumb-a
		$config_th['image_library'] = 'gd2';
		$config_th['source_image'] = $upload_path . $upload_image['file_name'];
		$config_th['create_thumb'] = FALSE;
		$config_th['maintain_ratio'] = TRUE;
		
		$config_th['new_image'] = $img_resize;
		// Duplo veci thumb
		$config_th['width'] = $config['thumb_width']*2;
			// Ista duzina i visina
		$config_th['height'] = $config['thumb_width']*2;
		// Ucitaj biblioteku
		$this->initialize($config_th);
		//$this->load->library('image_lib', $config_th);
		//Resize image
		if(	!$this->resize())
			 echo $this->display_errors();
		
		$img_size = getimagesize( $upload_path.$img_resize);
		$new_width = $img_size[0];
		$new_height = $img_size[1];
		//print_r($img_size);
		
		$config_cr['image_library'] = 'gd2';
		$config_cr['source_image'] =  $upload_path . $img_resize;
		
		$config_cr['new_image'] =   $img_thumb;
		$config_cr['create_thumb'] = TRUE;
		$config_cr['maintain_ratio'] = FALSE;
		
		$config_cr['x_axis'] =  $new_width/2 - $config['thumb_width']/2; 
		$config_cr['y_axis'] = $new_height/2 - $config['thumb_width']/2; 
		
		$config_cr['width'] = $config['thumb_width'];//190;
		// Ista duzina i visina
		$config_cr['height'] = $config['thumb_height'];//190; !!!
		
		
		$this->initialize($config_cr);
		
		//$this->crop(); // resize Thumb-a
		if ( ! $this->crop())
		{
		    echo $this->display_errors();
		}
		//obrisi privremenu sliku resize
		unlink($upload_path . $img_resize);
		$this->clear();
		///*
		//Promena velicine originalne slike
		
		// RESIZE
		
		$img_size = getimagesize($upload_path .  $upload_image['file_name']);
		$img_width = $img_size[0];
		$img_height = $img_size[1];
		
		$rw = $img_width / $config['image_width'];
		$rh = $img_height / $config['image_height'];
		
		if($rw<$rh)
			$r = $rw;
		else
			$r = $rh;
		
		$width = $img_width / $r;
		$height = $img_height / $r;
		
		$config_r['image_library'] = 'gd2';
		$config_r['source_image'] = $upload_path . $upload_image['file_name'];
		$config_r['new_image'] =   $upload_path . $upload_image['file_name'];
		$config_r['create_thumb'] = FALSE;
		$config_r['maintain_ratio'] = TRUE;
		
		$config_r['width'] = $width;//640;
		$config_r['height'] = $height;//480;

		
		$this->initialize($config_r);
		if(	!$this->resize())
			 echo $this->display_errors();
		//*/
		$this->clear();
		// CROP
		
		$img_size = getimagesize($upload_path .  $upload_image['file_name']);
		$img_width = $img_size[0];
		$img_height = $img_size[1];
		
		if( $img_width > $config['image_width'] )
		{
			$x = ($img_width - $config['image_width'] ) / 2;
			$y=0;
		}
		if($img_height > $config['image_height'])
		{
			$x=0;
			$y = ($img_height - $config['image_height']) / 2;
		}
		
		// sada crop-uj
		$config_c['image_library'] = 'gd2';
		$config_c['source_image'] =  $upload_path .$upload_image['file_name'];
		
		$config_c['new_image'] =   $upload_path .$upload_image['file_name'];
		$config_c['create_thumb'] = FALSE;
		$config_c['maintain_ratio'] = FALSE;
		
		$config_c['x_axis'] =  $x;
		$config_c['y_axis'] = $y;
		
		$config_c['width'] = $config['image_width'];//150;
			// Ista duzina i visina
		$config_c['height'] = $config['image_height'];//150; !!!
		
		
		$this->initialize($config_c);
		
		//$this->crop(); // resize Thumb-a
		if ( ! $this->crop())
		{
		    echo $this->display_errors();
		}
		$this->clear();
	}
	
	
	/**
	 * Menja velicinu i kreira thumb sliku
	 * @param string $image_path - putanja do fodlera sa slikom
	 * @param string $image_name - ime slike
	 * @param array $config - konfiguraciona podesavanja za resize i crop
	 */
	public function resize_crop($image_path, $image_name, $config)
	{
		if( ! @dir($image_path) ) // Direktorijum ne potoji
			return;  // nema dalje ako nema direktorijuma
		
		$thumb_dir = $image_path . "thumb";
		$thumb_path = $image_path . "thumb/"; // Kreiraj putanju za poddirektorijum thumb
		if( ! @dir($thumb_dir) ) // AKo ne postoji
		{
			mkdir($thumb_dir,0777,true); // kreiraj dir 'thumb'
			chmod($thumb_dir, 0777);
		}
		
		$full_image_name =  $image_path . $image_name; // Puno ime originalne slike
		$img_resize =  $image_path . "_resize_" . $image_name; // privremena resize slike za kreiranje thumb-a
		$img_thumb =  $thumb_path . $image_name; //strtolower($image_name); // thumb slika ima isto ime kao i originalana ali je u folderu thumb
		
		// Saznaj velicinu originalne slike
		$img_size = getimagesize( $full_image_name );
		
		//   KREIRANJE THUMB SLIKE
		
		if( $img_size[1] >  $img_size[0])
			$ratio = $img_size[1] / $img_size[0];
		else
			$ratio = $img_size[0] / $img_size[1];
		
		// Prvo smanji velicinu Slike na duplo vecu od stvarne velicine Thumb-a
		$config_th['image_library'] = 'gd2';
		$config_th['source_image'] = $full_image_name;
		$config_th['create_thumb'] = FALSE;
		$config_th['maintain_ratio'] = TRUE;
		
		$config_th['new_image'] = $img_resize;
		/*
		// Duplo veci thumb
		$config_th['width'] = $config['thumb_width'] * 2;
		// Ista duzina i visina
		$config_th['height'] = $config['thumb_width'] * 2;
		*/
		$config_th['width'] = $config['thumb_width'] * $ratio ;
		// Ista duzina i visina
		$config_th['height'] = $config['thumb_height'] * $ratio ;
		// nema ucitanja jer je ovo klasa izvedena iz biblioteke Image_lib, samo se vrsi inicijalizacija
		//$this->load->library('image_lib', $config_th);
		
		// Ucitaj podesavanja u biblioteku
		$this->initialize($config_th); 
		//Resize image, kreiranje slike duplo vece od thumb-a za kreiranje thumba
		if(	!$this->resize())
			echo $this->display_errors();
		$this->clear();
		
		$res_img_size = getimagesize( $img_resize );
		$res_width = $res_img_size[0];
		$res_height = $res_img_size[1];
		
		// PODESAVANJA ZA CROP-OVANJE RESIZE-OVANE SLIKE ZA THUMB
		$config_cr['image_library'] = 'gd2';
		$config_cr['source_image'] =   $img_resize;
		
		$config_cr['new_image'] =   $img_thumb;
		$config_cr['create_thumb'] = TRUE;
		$config_cr['maintain_ratio'] = FALSE;
		
		// Pozicija gornje leve tacke za crop
		$config_cr['x_axis'] =  $res_width/2 - $config['thumb_width'] / 2;
		$config_cr['y_axis'] = $res_height/2 - $config['thumb_width'] / 2;
		// Velicina za crop
		$config_cr['width'] = $config['thumb_width'];//190;
		// Ista duzina i visina
		$config_cr['height'] = $config['thumb_width'];//190; !!!
				
		$this->initialize($config_cr);
		
		//$this->crop(); // resize Thumb-a
		if ( ! $this->crop())
			echo $this->display_errors();
		$this->clear();
			
		
		//Promena velicine originalne slike
		
		
		
		$new_width = $img_size[0];  // duzina originalne slike
		$new_height = $img_size[1]; // visina originalne slike
		//print_r($img_size); // test
		// RESIZE
			
		// izracunaj odnose originalne slike za trazenim odnosom velicine slike koji je podesen u config
		$rw = $img_size[0] / $config['image_width']; 
		$rh = $img_size[1] / $config['image_height'];
		// Izracunaj mnozilac umanjenja/uvecanja originalne slike
		if($rw<$rh) // ako je sirina manje od visine
			$r = $rh; //onda je mnzilac odnos sirina originalne i destinaciono trazene velicine slike
		else
			$r = $rw; // ako ne onda je odnos visina mnozilac
		
		// Nova velicina slike [destinaciona slika]
		$width = $img_size[0] / $r; 
		$height = $img_size[1] / $r;
		
		$config_r['image_library'] = 'gd2';
		$config_r['source_image'] = $full_image_name;
		$config_r['new_image'] =   $full_image_name;
		$config_r['create_thumb'] = FALSE;
		$config_r['maintain_ratio'] = TRUE; // ipak sacuvaj oiginalni odnos duzine i visine
		
		$config_r['width'] = $width;
		$config_r['height'] = $height;
		
		
		$this->initialize($config_r);
		if(	!$this->resize())
			echo $this->display_errors();
		$this->clear();
		//*/
		/*
		//  Posto je secuvan originalni odnos visine i sirine 
		//  onda treba odseci malo sliku jer nije potrebnih dimenzija
		// Saznaj nove dimenzije
		$img_size = getimagesize( $full_image_name );
		// CROP RESIZE-ovane SLIKE
		// Pronadji koordinate gornjeg levog temena za crop
		if( $img_size[0] > $config['image_width'] )
		{
			$x = ( $img_size[0] - $config['image_width'] ) / 2;
			$y=0;
		}
		if( $img_size[1] >  $config['image_height'])
		{
			$x=0;
			$y = ( $img_size[1] - $config['image_height']) / 2;
		}
		
		// sada crop-uj
		$config_c['image_library'] = 'gd2';
		$config_c['source_image'] =  $full_image_name;
		
		$config_c['new_image'] =   $full_image_name;
		$config_c['create_thumb'] = FALSE;
		$config_c['maintain_ratio'] = FALSE; // sada vise ne drzi originalni odnos visine i sirine
		
		$config_c['x_axis'] = $x;
		$config_c['y_axis'] = $y;
		
		$config_c['width'] =  $config['image_width'];//150;
		// Ista duzina i visina
		$config_c['height'] =  $config['image_height'] ;//150; !!!
		
		
		$this->initialize($config_c);
		
		//$this->crop(); // resize Thumb-a
		if ( ! $this->crop())
			echo $this->display_errors();
		// */
		//obrisi privremenu sliku resize
		unlink( $img_resize);
	}
	
	/**
	 * F-ja koja pravi novu sliku od originalne.
	 * @param string $img_dir - direktorijum originalne slike 
	 * @param string $img_orig_name - naziv sliek u originalu
	 * @param string $dest_dir - odredisni direktorijum (ime slike ostaje isto)
	 * @param int $width - duzina slike
	 * @param int $height - visina slike
	 */
	public function image_resize_crop( $img_dir, $img_orig_name, $dest_dir, $width, $height)
	{
		if( ! @dir($img_dir) ) // Direktorijum ne potoji
			return;  // nema dalje ako nema direktorijuma
		
			
		$orig_img = $img_dir .'/'. $img_orig_name;
		
		// Saznaj velicinu originalne slike
		$img_size = getimagesize( $orig_img );
		
		//   KREIRANJE THUMB SLIKE
		
		if( $img_size[1] >  $img_size[0])
			$ratio = $img_size[1] / $img_size[0];
		else
			$ratio = $img_size[0] / $img_size[1];
		
		
		// Kreiranje mini_thumb-a
		//$dest_dir = $img_dir."/mini_thumb/";
		// Kreiraj folder ako ne postoji
		if( ! @dir($dest_dir) )
		{
			mkdir($dest_dir,0777,true);
			chmod($dest_dir, 0777);
		}
		// RESIZE SLIKE
		// Podesavanaj za novu sliku
		$config_th['image_library'] = 'gd2';
		$config_th['source_image'] = $orig_img;
		$config_th['create_thumb'] = FALSE;
		$config_th['maintain_ratio'] = TRUE;
		// Putanja i naziv nove slike 
		$config_th['new_image'] = $dest_dir . $img_orig_name;
		// Duzina nove slike
		//$config_th['width'] = $width * $ratio ;
		//$config_th['width'] = $width;
		// Visina nove slike
		//$config_th['height'] = $height * $ratio ;
		//$config_th['height'] = $height;

		//echo $ratio;
		
			if($img_size[1] <  $img_size[0]) // Landscape
			{
				if( 1 < ($width/$height) && ($width/$height) < 1.6  )
				{
					$config_th['width'] = $width ;//* $ratio;
					$config_th['height'] = $height ;// * $ratio;
				}
				else
				{
					$config_th['width'] = $width * $ratio;
					$config_th['height'] = $height * $ratio;
				}
				
			}
			elseif($img_size[1] >  $img_size[0]) // portrait
			{
				$config_th['width'] = $width * $ratio;
				$config_th['height'] = $height * $ratio * 3;
			}else // Square
			{
				$config_th['width'] = $width;
				$config_th['height'] = $height;
			}
		
		//print_r($config_th);
		// Ucitaj podesavanja u biblioteku
		$this->initialize($config_th);
		//Resize image, kreiranje slike duplo vece od thumb-a za kreiranje thumba
		if(	!$this->resize())
			echo $this->display_errors();
		$this->clear();
		///*
		// CROP SLIKE
		$res_img_size = getimagesize( $dest_dir . $img_orig_name );
		$res_width = $res_img_size[0];
		$res_height = $res_img_size[1];
		
		// PODESAVANJA ZA CROP-OVANJE RESIZE-OVANE SLIKE ZA THUMB
		$config_cr['image_library'] = 'gd2';
		$config_cr['source_image'] =   $dest_dir . $img_orig_name;
		
		$config_cr['new_image'] =   $dest_dir . $img_orig_name;
		$config_cr['create_thumb'] = TRUE;
		$config_cr['maintain_ratio'] = FALSE;
		
		// Pozicija gornje leve tacke za crop
		$config_cr['x_axis'] =  $res_width/2 - $width / 2;
		$config_cr['y_axis'] = $res_height/2 - $height / 2;
		// Velicina za crop
		$config_cr['width'] = $width;
		// Ista duzina i visina
		$config_cr['height'] = $height;
		
		$this->initialize($config_cr);
		
		//$this->crop(); // resize Thumb-a
		if ( ! $this->crop())
			echo $this->display_errors();
		$this->clear();
		// */
	}
	
	
	public function image_resize( $img_dir, $img_orig_name, $dest_dir, $width, $height)
	{
		if( ! @dir($img_dir) ) // Direktorijum ne potoji
			return;  // nema dalje ako nema direktorijuma
	
			
		$orig_img = $img_dir .'/'. $img_orig_name;
	
		// Saznaj velicinu originalne slike
		$img_size = getimagesize( $orig_img );
	
		//   KREIRANJE THUMB SLIKE
		/*
		if( $img_size[1] >  $img_size[0])
			$ratio = $img_size[1] / $img_size[0];
		else
			$ratio = $img_size[0] / $img_size[1];
		*/
	
		// Kreiranje mini_thumb-a
		//$dest_dir = $img_dir."/mini_thumb/";
		// Kreiraj folder ako ne postoji
		if( ! @dir($dest_dir) )
		{
			mkdir($dest_dir,0777,true);
			chmod($dest_dir, 0777);
		}
		// RESIZE SLIKE
		// Podesavanaj za novu sliku
		$config_th['image_library'] = 'gd2';
		$config_th['source_image'] = $orig_img;
		$config_th['create_thumb'] = FALSE;
		$config_th['maintain_ratio'] = TRUE;
		// Putanja i naziv nove slike
		$config_th['new_image'] = $dest_dir . $img_orig_name;
		// Duzina nove slike
		//$config_th['width'] = $width * $ratio ;
		//$config_th['width'] = $width;
		// Visina nove slike
		//$config_th['height'] = $height * $ratio ;
		//$config_th['height'] = $height;
		
		if( $img_size[0] >  $img_size[1] )	
		{
			$config_th['width'] = $width;
			$config_th['height'] = $width * ($img_size[0] /  $img_size[1]); 
		}
		else 
		{
			$config_th['height'] = $height;
			$config_th['width'] = $height / ($img_size[0] /  $img_size[1]); 
		}
		//echo $ratio;
		/*
		if($img_size[1] <  $img_size[0]) // Landscape
		{
			$config_th['width'] = $width * $ratio;
			$config_th['height'] = $height * $ratio;
		}
		elseif($img_size[1] >  $img_size[0]) // portrait
		{
			$config_th['width'] = $width * $ratio;
			$config_th['height'] = $height * $ratio * 3;
		}else // Square
		{
			$config_th['width'] = $width;
			$config_th['height'] = $height;
		}
		*/
		//print_r($config_th);
		// Ucitaj podesavanja u biblioteku
		$this->initialize($config_th);
		//Resize image, kreiranje slike duplo vece od thumb-a za kreiranje thumba
		if(	!$this->resize())
			echo $this->display_errors();
		$this->clear();
	}
	
}

?>
